package com.zl.learning.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;

public class TestXML2 {
    public static void main(String[] args) throws DocumentException, IOException {
        final String path = "C:\\Users\\zhaolei\\Documents\\WeChat Files\\styool\\FileStorage\\File\\2021-02\\JZ_ZIP_INDEX.xml";
        final String path2 = "C:\\Users\\zhaolei\\Documents\\WeChat Files\\styool\\FileStorage\\File\\2021-02\\JZ_ZIP_INDEX2.xml";
        SAXReader reader = new SAXReader();
        Document document = reader.read(path);
        Node node = document.selectSingleNode("/MESSAGE/DATASET/DATA/DATASET/DATA/DATASET/DATA/ITEM[@eng=\"RECORD_COUNT\"]");
        final Node node1 = node.selectSingleNode("@val");

        System.out.println(node1.getText());
        node1.setText("10");
        try (FileSystem fs = writeToZip("D:\\1.zip")) {
            XMLWriter writer = new XMLWriter(Files.newOutputStream(fs.getPath("/1.xml")), OutputFormat.createPrettyPrint());
            writer.write(document);
            writer.flush();
            writer.close();
        }

        final String text = node1.getText();
        System.out.println(text);
        Element root = document.getRootElement();
        System.out.println(root);
    }

    private static FileSystem writeToZip(String zipFile) throws IOException {
        Map<String, String> env = new HashMap<>();
        env.put("create", "true");
        env.put("encoding", "GBK");
        Path dist = Paths.get(zipFile);
        URI uri = URI.create("jar:file:" + dist.toUri().getPath());
        return FileSystems.newFileSystem(uri, env);
    }

    public static class Test2 {

        public static void main(String[] args) {
            String s = "\t";
            System.out.println(s.length());
        }
    }
}